import LayoutBasic from './layoutBasic';
import LoginLayout from './loginLayout';
import Redirect from 'umi/redirect';
import { message } from 'antd';

function BasicLayout(props) {
  const {
    location: { pathname },
  } = props;
  const RE = /^\/login/;

  /**
   * 判断权限
   */
  let permission = false;
  let resource = localStorage.getItem('resource')
    ? JSON.parse(localStorage.getItem('resource'))
    : [];

  for (let i = 0; i < resource.length; i++) {
    if (resource[i] && resource[i].childrens) {
      for (let j = 0; j < resource[i].childrens.length; j++) {
        let node = resource[i].childrens[j];
        let pathName = '';
        if (pathname.split('/').length > 2) {
          pathName = pathname
            .split('/')
            .slice(1, 3)
            .join('/');
        } else {
          pathName = pathname;
        }
        if (`/${node.sup}/${node.path}` == `/${pathName}`) {
          permission = true;
          break;
        } else if (pathName === '/login') {
          permission = true;
        }
      }
    }
  }

  if (permission || pathname === '/login') {
    return (
      <>{pathname.match(RE) ? <LoginLayout props={props} /> : <LayoutBasic props={props} />}</>
    );
  } else if (pathname === '/') {
    return <Redirect to="/login" />;
  } else {
    message.error('没有权限');
    return <Redirect to="/login" />;
  }
}

export default BasicLayout;
